<?php


namespace App\Models;


use App\User;
use Illuminate\Database\Eloquent\Model;

class UserMoneyLog extends Model
{
    protected $table = 'user_money_log';

    public function users(){
        return $this->belongsTo(User::class,'user_id','id');
    }

    public static function createLog(User $user, array $params)
    {
        if ($params['type'] == 1) {
            $before = $user['money'] - $params['change'];
        } else {
            $before = $user['money'] + $params['change'];
        }
        $log = [
            'user_id' => $user['id'],
            'before' => $before,
            'change' => $params['change'],
            'after' => $user['money'],
            'type' => $params['type'],
            'desc' => $params['desc'],
        ];
        $model = new self();
        foreach ($log as $key=>$val) {
            if (!empty($val)) {
                $model->$key = $val;
            }
        }
        return $model->save();
    }
}